<template>
    <div class="inspection-site-progress-layout">
        <div v-if="isShowBDMapState" id="idBaiDuMap" class="biadu-map-part"></div>
        <yd-timeline>
            <yd-infinitescroll :callback="loadList" ref="infinitescrollDemo">
                <div slot="list">
                    <yd-timeline-item v-for="(item,index) in progressList">
                        <div>
                            <div class="inspection-progress-content-part">
                                <span class="title-part">{{item.state_name}}</span>
                                <span class="time-part">{{item.created.substring(0,item.created.lastIndexOf(":"))}}</span>
                            </div>
                            <div v-if="item.state==='10'" class="inspection-progress-people-part">
                                <span class="blue-color">{{item.customer_name}}</span>
                                <!--<i v-show="isCanShowPhoneLogo" @click="telHandlerClick(item.customer_mobile)" slot="icon" class="iconfont icon-dianhua1 blue-color"></i>-->
                                <img v-show="isCanShowPhoneLogo" @click="telHandlerClick(item.customer_mobile)" slot="icon" src="../../assets/icon-dianhua1.png" class="phone-logo-part">
                            </div>
                            <div v-if="item.state==='99'" class="inspection-progress-people-part">
                                <span class="gray-tint-color">{{item.content}}</span>
                            </div>
                        </div>
                    </yd-timeline-item>
                </div>
                <span slot="doneTip"></span>
            </yd-infinitescroll>
        </yd-timeline>

        <div style="height: 1.4rem"></div>

        <div class="inspection-operation-part buttom-tabbar">
            <!--评价按钮-->
            <a  href="javascript:;"
                @click="evaluateHandleClick"
                v-show="isCanCloseEvaluate"
                class="weui-btn weui-btn_primary horizontal-part">评价</a>
        </div>

        <yd-popup v-model="isShowEvaluateDialog" position="bottom" height="70%">
            <div class="mui-flex">
                <div class="cell">
                    <img :src="qn_domain+headUrl" class="img-logo" alt=""  @click="show1 = true"
                         size="large">
                </div>
                <div class="cell4">
                    <p class="center-name">{{userName}}</p>
                </div>
            </div>
            <yd-cell-item>
                <span slot="left">专业技能</span>
                <yd-rate slot="right" v-model="jineng" :showText="['很差','较差','一般','较好','很好']"></yd-rate>
            </yd-cell-item>
            <yd-cell-item>
                <span slot="left">响应速度</span>
                <yd-rate slot="right" v-model="sudu" :showText="['很差','较差','一般','较好','很好']"></yd-rate>
            </yd-cell-item>
            <yd-cell-item>
                <span slot="left">服务态度</span>
                <yd-rate slot="right" v-model="fuwu" :showText="['很差','较差','一般','较好','很好']"></yd-rate>
            </yd-cell-item>
            <div class="small-space-background"></div>
            <yd-cell-item>
                <yd-textarea slot="right" placeholder="详细评价" maxlength="200" v-model="desContent"></yd-textarea>
            </yd-cell-item>
            <div class="small-space-background"></div>
            <div class="evaluate-dialog-part" style="background-color:#f3f8f8">
                <yd-button size="large" type="primary" @click.native="repairEvaluateClick" style="background-color:#3cc8a8;color:white;height: 50px">确定</yd-button>
            </div>
        </yd-popup>

    </div>
</template>

<script>
    import {getProLocation,pagedOperations,getProInfoFromOrder,sendEvaluate} from "../../pcof/post";
    import {userInfoData,bdLatitude,bdLongitude,qn_domain} from '../../globalVariables'
    import util from '../../util.js';
    export default {
        name: "inspectionSiteProgress",
        data() {
            return {
                isShowBDMapState: false,
                progressList: [],
                taskUUid: '',
                page: 1,
                pageSize: 20,
                serviceTotalPage: 1,
                headUrl: '',
                //评价用户名称
                userName: '',
                //七牛Url
                qn_domain: '',
                //控制显示评价Dialog
                isShowEvaluateDialog: false,
                //控制评价按钮
                isCanCloseEvaluate: false,
                //专业技能
                jineng: 1,
                //响应速度
                sudu: 1,
                //服务态度
                fuwu: 1,
                //评价内容
                desContent: '',
                isCanShowPhoneLogo: true,
                instLatitude: 0,
                instLongitude: 0,
                proLatitude: '',
                proLongitude: '',
            }
        },
        async created() {
            //接受上个页面传值 在实例创建完成后被立即调用
            this.taskUUid = this.$route.params.taskUUid;
            this.taskState = this.$route.params.taskState;
            this.isShowEvaluateDialog = this.$route.params.isShowEvaluateDialog;

            //报修机构的地理位置
            this.instLatitude = localStorage.getItem(bdLatitude); // 纬度，浮点数，范围为90 ~ -90
            this.instLongitude = localStorage.getItem(bdLongitude); // 经度，浮点数，范围为180 ~ -180。

            if (this.taskState === '90') {
                this.isCanCloseEvaluate = true;
            }

            if (this.taskState === '90' || this.taskState === '98' || this.taskState === '99') {
                this.isCanShowPhoneLogo = false;
            } else {
                this.isCanShowPhoneLogo = true;
            }

            this.qn_domain = localStorage.getItem(qn_domain);
            this.$dialog.loading.open('加载中...');
            var response = await pagedOperations(
                JSON.parse(localStorage.getItem(userInfoData)).mobile,
                this.taskUUid,
                1,
                20);
            if (response.data.code === 200) {
                this.$dialog.loading.close();
                this.progressList = response.data.data.list;
                if (this.progressList.state === '02' || this.progressList.state === '10') {
                    this.isShowBDMapState = true;
                }
                this.serviceTotalPage = response.data.data.totalPage;
                if (this.serviceTotalPage > 1) {
                    this.page++;
                } else {
                    /* 所有数据加载完毕 */
                    this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                }
            } else {
                this.$dialog.loading.close();
                this.$dialog.toast({
                    mes: response.data.msg,
                    timeout: 1500
                });
            };
            console.log(this.qn_domain)
            //获取该任务下运维任务信息
            this.getProInfoFromOrderFunc();
        },
        mounted() {
            // this.showBaiDuMapFunc();
        },
        methods: {
            evaluateHandleClick() {
                this.isShowEvaluateDialog = true;
            },
            async repairEvaluateClick() {
                var response = await sendEvaluate(
                    JSON.parse(localStorage.getItem(userInfoData)).mobile,
                    this.taskUUid,
                    this.jineng*2,
                    this.sudu*2,
                    this.fuwu*2,
                    JSON.parse(localStorage.getItem(userInfoData)).uuid,
                    this.desContent
                );
                if (response.data.code === 200) {
                    this.$router.back();
                } else {
                    this.$dialog.toast({
                        mes: response.data.msg,
                        timeout: 1500
                    });
                }
            },
            async getProLocationFunc(uuid) {
                var response = await getProLocation(uuid);
                if (response.data.code === 200) {
                    this.proLatitude = response.data.data.latitude;
                    this.proLongitude = response.data.data.longitude;
                    if (this.isShowBDMapState) {
                        this.showBaiDuMapFunc();
                    }
                }
            },
            async getProInfoFromOrderFunc() {
                var response = await getProInfoFromOrder(
                    JSON.parse(localStorage.getItem(userInfoData)).mobile,
                    this.taskUUid,
                );
                if (response.data.code === 200) {
                    this.headUrl = response.data.data.head_image_url;
                    this.userName = response.data.data.name;
                    // if (this.isShowBDMapState) {
                    //     this.showBaiDuMapFunc();
                    // }
                    this.getProLocationFunc(response.data.data.uuid);
                } else {
                    this.$dialog.toast({
                        mes: response.data.msg,
                        timeout: 1500
                    });
                }
            },
            showBaiDuMapFunc() {
                let that = this;
                var map;
                if (this.instLatitude === null || this.instLatitude === '') {
                } else {
                    // 百度地图API功能
                    // 创建Map实例
                    //报修单位置
                    map = new BMap.Map("idBaiDuMap", {enableMapClick: true});
                    // 初始化地图,设置中心点坐标和地图级别
                    var point = new BMap.Point(parseFloat(this.instLongitude), parseFloat(this.instLatitude));
                    map.centerAndZoom(point, 13);
                    // 添加地图类型控件
                    map.addControl(new BMap.MapTypeControl());
                    // 创建标注
                    var marker = new BMap.Marker(point);
                    map.addOverlay(marker);

                    function ComplexCustomOverlay(point) {
                        this._point = point;
                    };
                    ComplexCustomOverlay.prototype = new BMap.Overlay();
                    ComplexCustomOverlay.prototype.initialize = function(map){
                        this._map = map;
                        var div = this._div = document.createElement("div");
                        div.style.position = "absolute";
                        div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);//聚合功能?
                        // 可以根据参数设置元素外观
                        // div.style.width="0.8rem";
                        // div.style.height = "0.6rem";
                        var arrow = this._arrow = document.createElement("img");
                        console.log('headUrl');
                        console.log(that.headUrl);
                        if (that.headUrl === null || that.headUrl === '' || that.headUrl === undefined) {
                            arrow.src = require('../../assets/ic_avatar_default.png');
                        } else {
                            arrow.src = that.qn_domain+that.headUrl;
                        }
                        arrow.style.width = "0.6rem";
                        arrow.style.height = "0.6rem";
                        arrow.style.top = "22px";
                        arrow.style.left = "15px";
                        arrow.style.borderRadius = "0.6rem";
                        arrow.style.objectFit = "cover";
                        div.appendChild(arrow);
                        map.getPanes().markerPane.appendChild(div);//getPanes(),返回值:MapPane,返回地图覆盖物容器列表  labelPane呢???
                        return div;
                    };

                    ComplexCustomOverlay.prototype.draw = function(){
                        var map = this._map;
                        var pixel = map.pointToOverlayPixel(this._point);
                        this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
                        this._div.style.top  = pixel.y - 26 + "px";
                    };

                    //多点标记
                    var secondpoint = new BMap.Point(parseFloat(this.proLongitude), parseFloat(this.proLatitude));
                    map.centerAndZoom(secondpoint, 13);
                    // 添加地图类型控件
                    map.addControl(new BMap.MapTypeControl());
                    // 创建标注
                    // var secondmarker = new BMap.Marker(secondpoint);
                    // map.addOverlay(secondmarker);
                    var myCompOverlay = new ComplexCustomOverlay(secondpoint);
                    map.addOverlay(myCompOverlay);//将标注添加到地图中

                    // wx.ready(function () {
                    //     wx.getLocation({
                    //         type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
                    //         success: function (res) {
                    //             var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
                    //             var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
                    //             var speed = res.speed; // 速度，以米/每秒计
                    //             var accuracy = res.accuracy; // 位置精度
                    //
                    //             //多点标记
                    //             var secondpoint = new BMap.Point(longitude, latitude);
                    //             map.centerAndZoom(secondpoint, 13);
                    //             // 添加地图类型控件
                    //             map.addControl(new BMap.MapTypeControl());
                    //             // 创建标注
                    //             // var secondmarker = new BMap.Marker(secondpoint);
                    //             // map.addOverlay(secondmarker);
                    //             var myCompOverlay = new ComplexCustomOverlay(secondpoint);
                    //             map.addOverlay(myCompOverlay);//将标注添加到地图中
                    //         }
                    //     });
                    // });
                }
            },
            async loadList() {
                var response = await pagedOperations(
                    JSON.parse(localStorage.getItem(userInfoData)).mobile,
                    this.taskUUid,
                    this.page,
                    this.pageSize);
                if (response.data.code === 200) {
                    const _list = response.data.data.list;
                    this.progressList = [...this.progressList, ..._list];
                    if (_list.length < this.pageSize || this.page === this.serviceTotalPage) {
                        /* 所有数据加载完毕 */
                        this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.loadedDone');
                        return;
                    }
                    /* 单次请求数据完毕 */
                    this.$refs.infinitescrollDemo.$emit('ydui.infinitescroll.finishLoad');

                    this.page++;

                } else {

                }
            },
            telHandlerClick(number) {
                window.location.href = 'tel://'+number;
            }
        },
    }
</script>

<style lang="scss">
    .inspection-site-progress-layout {
        padding-top: 1rem;
        .biadu-map-part {
            width: 100%;
            height: 4rem;
        }

        .inspection-progress-content-part {
            display: flex;
            justify-content: space-between;

            .title-part {
                font-size: 1.2em;
                color: #0d0d0d
            }

            .time-part {
                color: #6e6e6e;
            }

        }

        .inspection-progress-people-part {
            display: flex;
            padding-top: 0.2rem;

            .iconfont {
                font-size: 0.3rem;
                padding-left: 0.1rem;
            }

            .phone-logo-part {
                width: 0.3rem;
                height: 0.3rem;
                margin-left: 0.1rem;
            }
        }

        .inspection-operation-part{
            padding-top: 0.3rem;
            padding-bottom: 0.3rem;
            display: flex;
            z-index:200;
            background: #fafafa;

            .horizontal-part {
                margin: 0 auto;
                width: 40%;
            }

            .weui-btn {
                display: flex;
                height: 0.6rem;
                align-items: center;
                justify-content: center;
                font-size: 1.2em;
            }
        }
    }
</style>